package com.le.tester.tip;

import com.alibaba.fastjson.JSONObject;
import io.agora.media.RtcTokenBuilder;
import org.apache.commons.lang3.StringUtils;
import org.apache.http.NameValuePair;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.entity.StringEntity;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClientBuilder;
import org.apache.http.impl.client.LaxRedirectStrategy;
import org.apache.http.message.BasicNameValuePair;
import org.apache.http.util.EntityUtils;

import java.nio.charset.StandardCharsets;
import java.sql.CallableStatement;
import java.util.ArrayList;
import java.util.List;
import java.util.Properties;
import java.util.UUID;

/**
 * createTime：2021/9/7 14:11
 * description：post远程调用接口
 */
public class HttpUse {
    public static void doPostParam() throws Exception {
        // 创建Httpclient对象
        CloseableHttpClient httpclient = HttpClientBuilder.create().setRedirectStrategy(new LaxRedirectStrategy()).build();

        // 创建http POST请求
        HttpPost httpPost = new HttpPost("https://a1.easemob.com/1146161212178788/fireim/token");

        //List<NameValuePair> parameters = new ArrayList<NameValuePair>();
        //{"grant_type":"client_credentials","client_id":"YXA6r3AJ_5OQRiOLoctl_5B7Jw","client_secret":"YXA6D2UPBwRqx_VFIlgMn95PSivcQg8"}
//        parameters.add(new BasicNameValuePair("grant_type", "client_credentials"));
//        parameters.add(new BasicNameValuePair("client_id", "YXA6r3AJ_5OQRiOLoctl_5B7Jw"));
//        parameters.add(new BasicNameValuePair("client_secret", "YXA6D2UPBwRqx_VFIlgMn95PSivcQg8"));
//        // 构造一个form表单式的实体
//        UrlEncodedFormEntity formEntity = new UrlEncodedFormEntity(parameters);
//        // 将请求实体设置到httpPost对象中
//        httpPost.setEntity(formEntity);
//        httpPost.setHeader("content-type", "application/json;charset=UTF-8");


        JSONObject jsonParam = new JSONObject();
        jsonParam.put("grant_type", "client_credentials");
        jsonParam.put("client_id", "YXA6r3AJ_5OQRiOLoctl_5B7Jw");
        jsonParam.put("client_secret", "YXA6D2UPBwRqx_VFIlgMn95PSivcQg8");

        StringEntity entity = new StringEntity(jsonParam.toString(), StandardCharsets.UTF_8);//解决中文乱码问题
        entity.setContentEncoding("UTF-8");
        entity.setContentType("application/json");

        httpPost.setEntity(entity);

        CloseableHttpResponse response = null;
        try {
            // 执行请求
            response = httpclient.execute(httpPost);
            System.out.println(response.getStatusLine());
            // 判断返回状态是否为200
            if (response.getStatusLine().getStatusCode() == 200) {
                String content = EntityUtils.toString(response.getEntity(), "UTF-8");
                System.out.println(content);
            }
        } finally {
            if (response != null) {
                response.close();
            }
            httpclient.close();
        }
    }

    public static void main(String[] args) throws Exception {
//        RtcTokenBuilder rtcTokenBuilder = new RtcTokenBuilder();
//        long expTime = System.currentTimeMillis() / 1000 + 1;
//        String resultToken = rtcTokenBuilder.buildTokenWithUid("5add38694825482eafc9b0bd1f313b1f", "9c561b54bcc241909d39dfe4fc12f549",
//                String.valueOf(UUID.randomUUID()), 0, RtcTokenBuilder.Role.Role_Publisher, Math.toIntExact(expTime));
//        System.out.println(resultToken);
    }
}
